package com.wss.common.widget;

import com.wss.common.base.ResourceTable;
import com.wss.common.utils.ValidUtils;
import com.wss.common.widget.attrs.StyleEnhanceEditText;
import ohos.agp.components.*;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;
import ohos.global.resource.NotExistException;

import java.io.IOException;

public class EnhanceEditText extends DirectionalLayout {

    /**
     * 无操作
     */
    public static final String ACTION_NONE = "none";
    /**
     * 搜索事件
     */
    public static final String ACTION_SEARCH = "search";
    /**
     * 下一步
     */
    public static final String ACTION_NEXT = "next";
    /**
     * 完成
     */
    public static final String ACTION_DONE = "done";

    /**
     * 输入文本
     */
    public static final String INPUT_TEXT = "text";
    /**
     * 输入密码
     */
    public static final String INPUT_PASSWORD = "password";

    /**
     * 输入手机
     */
    public static final String INPUT_PHONE = "phone";
    /**
     * 输入数字
     */
    public static final String INPUT_NUMBER = "number";


//    @BindComponent(ResourceTable.Id_ll_edt_view)
    DirectionalLayout llEdtView;

//    @BindComponent(ResourceTable.Id_iftv_left)
    IconFontTextView iftvLeft;

//    @BindComponent(ResourceTable.Id_edt_text)
    TextField editText;

//    @BindComponent(ResourceTable.Id_iftv_clean)
    IconFontTextView iftvClean;

//    @BindComponent(ResourceTable.Id_ll_clean_view)
    Component cleanView;

    private int textColor = 0xFF333333;
    private int hintColor = 0XFF999999;
    private int leftIconColor = 0XFF999999;
    private int cleanIconColor = 0XFF999999;
    private String leftIcon;
    private String cleanIcon;
    private String hintText;
    private int textSize = 14;
    /**
     * 背景资源
     */
    private int bgResource = ResourceTable.Graphic_bg_of_color_f5_4radius;
    /**
     * 是否显示清除按钮 默认true
     */
    private boolean showCleanIcon = true;
    /**
     * 是否现在左侧icon 默认true
     */
    private boolean showLeftIcon = true;
    /**
     * 是否显示背景 默认true
     */
    private boolean showBackground = true;
    /**
     * 键盘最后执行的Action
     */
    private String action = ACTION_NONE;
    /**
     * 输入框类型
     */
    private String inputType = INPUT_TEXT;
    private int maxLength;
    private int maxLines;

    private OnTextChangeListener onTextChangeListener;
    private OnActionClickListener onActionClickListener;

    public EnhanceEditText(Context context) {
        this(context, null);
    }

    public EnhanceEditText(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public EnhanceEditText(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        textColor = attrSet.getAttr(StyleEnhanceEditText.eTextColor).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eTextColor).get().getIntegerValue() : textColor;
        hintColor = attrSet.getAttr(StyleEnhanceEditText.eHintTextColor).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eHintTextColor).get().getIntegerValue() : hintColor;
        leftIcon = attrSet.getAttr(StyleEnhanceEditText.eLeftIconFont).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eLeftIconFont).get().getStringValue() : leftIcon;
        leftIconColor = attrSet.getAttr(StyleEnhanceEditText.eLeftIconColor).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eLeftIconColor).get().getIntegerValue() : leftIconColor;
        cleanIcon = attrSet.getAttr(StyleEnhanceEditText.eCleanIconFont).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eCleanIconFont).get().getStringValue() : cleanIcon;
        cleanIconColor = attrSet.getAttr(StyleEnhanceEditText.eCleanIconColor).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eCleanIconColor).get().getIntegerValue() : cleanIconColor;

        hintText = attrSet.getAttr(StyleEnhanceEditText.eHintText).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eHintText).get().getStringValue() : hintText;
        textSize = attrSet.getAttr(StyleEnhanceEditText.eTextSize).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eTextSize).get().getIntegerValue() : textSize;

        showCleanIcon = attrSet.getAttr(StyleEnhanceEditText.eShowClean).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eShowClean).get().getBoolValue() : showCleanIcon;

        bgResource = attrSet.getAttr(StyleEnhanceEditText.eBg).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eBg).get().getIntegerValue() : bgResource;

        showLeftIcon = attrSet.getAttr(StyleEnhanceEditText.eShowLeft).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eShowLeft).get().getBoolValue() : showLeftIcon;

        showBackground = attrSet.getAttr(StyleEnhanceEditText.eShowBg).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eShowBg).get().getBoolValue() : showBackground;

        action = attrSet.getAttr(StyleEnhanceEditText.eAction).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eAction).get().getStringValue() : action;

        inputType = attrSet.getAttr(StyleEnhanceEditText.eInputType).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eInputType).get().getStringValue() : inputType;

        maxLength = attrSet.getAttr(StyleEnhanceEditText.eMaxLength).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eMaxLength).get().getIntegerValue() : maxLength;

        maxLines = attrSet.getAttr(StyleEnhanceEditText.eMaxLines).isPresent() ?
                attrSet.getAttr(StyleEnhanceEditText.eMaxLines).get().getIntegerValue() : maxLines;

        findView();

        initView();
    }

    private void findView() {
        llEdtView = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_edt_view);
        iftvLeft = (IconFontTextView) findComponentById(ResourceTable.Id_iftv_left);
        editText = (TextField) findComponentById(ResourceTable.Id_edt_text);
        iftvClean = (IconFontTextView) findComponentById(ResourceTable.Id_iftv_clean);
        cleanView = findComponentById(ResourceTable.Id_ll_clean_view);

        cleanView.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                onClean();
            }
        });
    }

    private void initView() {
        setAlignment(LayoutAlignment.VERTICAL_CENTER);
        if (showBackground) {
            try {
                PixelMapElement element = new PixelMapElement(getContext()
                        .getResourceManager()
                        .getResource(bgResource));
                setBackground(element);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            }
        }
        if (showLeftIcon && ValidUtils.isValid(leftIcon)) {
            iftvLeft.setValue(leftIcon);
            iftvLeft.setTextColor(new Color(leftIconColor));
        } else {
            iftvLeft.setVisibility(VISIBLE);
        }
        if (showCleanIcon && ValidUtils.isValid(cleanIcon)) {
            iftvClean.setValue(cleanIcon);
            iftvClean.setTextColor(new Color(cleanIconColor));
        }
        editText.setHint(hintText);
        editText.setTextSize(textSize);
        editText.setTextColor(new Color(textColor));
        editText.setHintColor(new Color(hintColor));
        editText.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String text, int start, int before, int count) {
                if (onTextChangeListener != null) {
                    onTextChangeListener.onTextUpdated(text, start, before, count);
                }
            }
        });

        setInputFiller();
        setOnEditorAction();
    }

    /**
     * 设置输入框的一些属性
     */
    private void setInputFiller() {
        switch (inputType) {
            case INPUT_PASSWORD:
                editText.setTextInputType(InputAttribute.PATTERN_PASSWORD);
                break;
            case INPUT_PHONE:
                editText.setTextInputType(InputAttribute.PATTERN_NUMBER);
                break;
            case INPUT_NUMBER:
                editText.setTextInputType(InputAttribute.PATTERN_NUMBER);
                break;
            case INPUT_TEXT:
            default:
                editText.setTextInputType(InputAttribute.PATTERN_TEXT);
                break;
        }
        if (maxLength > 0) {
            //TODO edit text set filters
//            editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        }
        if (maxLines > 0) {
            editText.setMaxTextLines(maxLines);
        }
    }

    /**
     * 设置输入框的执行事件
     */
    private void setOnEditorAction() {
        switch (action) {
            case ACTION_SEARCH:
                editText.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEARCH);
                break;
            case ACTION_NEXT:
                editText.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_GO);
                break;
            case ACTION_DONE:
                editText.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_SEND);
                break;
            case ACTION_NONE:
            default:
                editText.setInputMethodOption(InputAttribute.ENTER_KEY_TYPE_UNSPECIFIED);
                break;
        }

        editText.setEditorActionListener(new Text.EditorActionListener() {
            @Override
            public boolean onTextEditorAction(int action) {
                //TODO 隐藏键盘
//                KeyboardUtils.hideKeyboard(v);
                if (onActionClickListener == null) {
                    return false;
                }
                switch (action) {
                    case InputAttribute.ENTER_KEY_TYPE_SEARCH:
                        //搜索
                        onActionClickListener.onEditorAction(action);
                        break;
                    case InputAttribute.ENTER_KEY_TYPE_GO:
                        //下一步
                        onActionClickListener.onEditorAction(action);
                        break;
                    case InputAttribute.ENTER_KEY_TYPE_SEND:
                        //完成
                        onActionClickListener.onEditorAction(action);
                        break;
                    case InputAttribute.ENTER_KEY_TYPE_UNSPECIFIED:
                    default:
                        //无操作
                        onActionClickListener.onEditorAction(action);
                        break;
                }
                return true;
            }
        });

    }

//    @OnClick(ResourceTable.Id_ll_clean_view)
    public void onClean() {
        getEditText().setText("");
    }

    /**
     * 返回输入框字符
     *
     * @return String
     */
    public String getText() {
        return getEditText().getText().toString().trim();
    }

    /**
     * 清除搜索框内容
     */
    public void clearSearchText() {
        onClean();
    }

    /**
     * 返回输入框View
     *
     * @return EditText
     */
    public TextField getEditText() {
        return editText;
    }

    /**
     * 设置搜索按钮点击回调
     *
     * @param onActionClickListener OnEditActionCallback
     */
    public void setOnActionClickListener(OnActionClickListener onActionClickListener) {
        this.onActionClickListener = onActionClickListener;
    }

    /**
     * 设置文本输入变化监听
     *
     * @param textChangeListener listener
     */
    public void setTextChangeListener(OnTextChangeListener textChangeListener) {
        this.onTextChangeListener = textChangeListener;
    }

    /**
     * 设置文本
     *
     * @param text 文本
     */
    public void setText(String text) {
        editText.setText(text);
    }

    /**
     *TODO 设置输入框光标位置
     *
     * @param position 位置
     */
    public void setSelection(int position) {
//        editText.setSelection(position);
    }

    /**
     * 文本输入变化监听
     */
    public interface OnTextChangeListener {
        void onTextUpdated(String text, int start, int before, int count);
    }

    /**
     * 键盘搜索按钮点击
     */
    public interface OnActionClickListener {
        /**
         * 按钮被点击
         *
         * @param action 事件ID
         */
        void onEditorAction(int action);
    }
}
